const mongoose = require('mongoose')

//连接 mongodb服务
mongoose.connect('mongodb://127.0.0.1:27017/bibi')

mongoose.connection.on('open',()=>{
    console.log('连接成功')
     let BookSchema = new mongoose.Schema({
        name:String,
        author:String,
        price:mongoose.Schema.Types.String
     });

     let BookModel = mongoose.model('books',BookSchema);

     // insert
     // BookModel.create({
     //    name:'西游记',
     //    author:"吴承恩",
     //    price:19.9
     // },(err,book)=>{
     //     if (err){
     //         console.log(err)
     //         return
     //     }
     //     console.log(err);
     // })

    // delete
    // BookModel.deleteOne({_id:'686681accf642cf7f5689fb8'  },(err)=>{
    //     console.log(err);
    // })

    // update
    // BookModel.updateOne({name:'西游记'},{price:'10'},(err,data)=>{
    //     if (err) {
    //         console.log(err);
    //     }
    //     console.log(data)
    // })


    // BookModel.deleteMany({name:'西游记'},{price:'10'},(err,data)=>{
    //     if (err) {
    //         console.log(err);
    //     }
    //     console.log(data)
    // })

    BookModel.findOne({name:'西游记'},(err,books)=>{
        if (err){
            console.log(err)
        }
        console.log(books);
        
    })

})

mongoose.connection.on('error',()=>{
    console.log('连接失败') 
});

mongoose.connection.on('close',()=>{
    console.log('连接关闭')
});